<template>
  <div>
    <el-container>
      <!-- Header -->
      <el-header class="homeHeader">
        <div class="title">
          人力资源管理系统
        </div>
        <div>
          <el-button icon="el-icon-bell"
                     size="normal"
                     style="margin-right: 10px;color: #eecf1f"
                     type="text"
                     @click="goChat">

          </el-button>
          <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link">
            <span class="userName">{{ user.name }}</span>
            <img :src="user.userFace">
          </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="userInfo">个人中心</el-dropdown-item>
              <el-dropdown-item command="setting">设置</el-dropdown-item>
              <el-dropdown-item command="logout">注销登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <!-- Aside -->
      <el-container>
        <el-aside width="200px">
          <!-- router 开启路由绑定 $router.options.routes 获取所有路由-->
          <el-menu router unique-opened >
            <el-submenu v-for="(item, index) in routes"
                        :key="index"
                        :index="index+''"
                        v-if="!item.hidden">
              <template slot="title">
                <i :class="item.iconCls" style="color: #eecf1f; margin-right: 5px"></i>
                <span class="nameSpan">{{ item.name }}</span>
              </template>
              <el-menu-item :index="children.path"
                            v-for="(children, index_son) in item.children">
                {{ children.name }}
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- Main -->
        <el-main>
          <el-breadcrumb separator-class="el-icon-arrow-right"
                         v-if="this.$router.currentRoute.path!='/home'">
            <el-breadcrumb-item :to="{ path: '/home' }">人力资源管理系统</el-breadcrumb-item>
            <el-breadcrumb-item>{{ this.$router.currentRoute.name }}</el-breadcrumb-item>
          </el-breadcrumb>
          <div id="homeWelcome" v-if="this.$router.currentRoute.path=='/home'">
            欢迎使用人力资源管理系统
          </div>
          <router-view class="homeRouterView"/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      // user: JSON.parse(window.sessionStorage.getItem('user'))
    }
  },
  computed: {
    routes() {
      return this.$store.state.routes;
    },
    user() {
      return this.$store.state.currentAdmin;
    }
  },
  methods: {
    goChat() {
      this.$router.push('/chat');
    },
    handleCommand(command) {
      //注销登录
      if (command == 'logout') {
        this.$confirm('此操作将注销登录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.postRequest('/logout').then(resp => {
            console.log(resp);
          });
          //清空登录信息
          window.sessionStorage.removeItem('tokenStr');
          window.sessionStorage.removeItem('user');
          //清空菜单缓存
          this.$store.commit('initRoutes', []);
          //页面跳转
          this.$router.replace('/');
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      }
      if (command == 'userInfo') {
        this.$router.push('/userInfo');
      }
    }
  }
}
</script>

<style scoped>
.homeHeader {
  background: #dc191d;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  box-sizing: border-box;
}

.homeHeader .title {
  font-size: 30px;
  font-family: 华文新魏;
  color: white;
}
.homeHeader .userInfo {
  cursor: pointer;
}
.el-dropdown-link img {
  width: 48px;
  height: 48px;
  border-radius: 24px;
  margin-left: 10px;
}
.userName {
  color: white;
  font-size: 16px;
  font-family: 仿宋;
  text-align: center;
}
.nameSpan {
  color: #4e4e4e;
}
.homeRouterView {
  margin-top: 15px;
}
#homeWelcome {
  font-family: 华文仿宋;
  font-size: 30px;
  text-align: center;
}
</style>